package com.spark.check

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.view.View
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import kotlinx.android.synthetic.main.frag_invite.*

/**
 * 特邀人员
 */
class InviteFragment : BaseFragment() {
    override fun contentResId(): Int {
        return R.layout.frag_invite
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val tmp = mutableListOf("1部分", "2部分", "3部分", "4部分", "5部分")
        tmp.forEach {
            tabs.addTab(tabs.newTab().setText(it))
        }
        activity?.apply {
            val invitePageAdapter = InvitePageAdapter(supportFragmentManager, tmp)
            pager.adapter = invitePageAdapter
            tabs.setupWithViewPager(pager)
        }
    }
}

class InvitePageAdapter(fm: FragmentManager, var tabs: MutableList<String>) : FragmentStatePagerAdapter(fm) {
    override fun getItem(position: Int): Fragment {
        return InviteChildFragment.newInstance(position)
    }

    override fun getCount(): Int {
        return tabs.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return tabs[position]
    }

}

class InviteChildFragment : BaseFragment() {
    private var index = 0

    companion object {
        fun newInstance(index: Int): InviteChildFragment {
            val fragment = InviteChildFragment()
            val args = Bundle()
            args.putInt(Const.tag_index, index)
            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_child_invite
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        index = arguments?.getInt(Const.tag_index, 0) ?: 0
    }

    private fun fetchGuests() {
        val params = mutableMapOf(
                Pair(Const.qr_taskid, "$id")
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_task_check_invite_list}", params)
                    .wrapIO()
                    .subscribe(
                            {},
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading()
                            }
                    )
        }
    }
}